package com.service.impl;

import com.bean.Payment;
import com.dao.impl.PaymentDAOImpl;
import com.service.PaymentService;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class PaymentServiceImpl implements PaymentService {

    PaymentDAOImpl paymentDAO = new PaymentDAOImpl();
    @Override
    public List<Payment> selectAll() throws SQLException {
        return paymentDAO.selectAll();
    }
    @Override
    public List<Payment>  selectByName(String ownerId) throws SQLException {
        return paymentDAO.selectByName(ownerId);
    }

    /**
     * 查询余额和时间
     * @param ownerId
     * @param paymentType
     * @return
     * @throws SQLException
     */
    @Override
    public Map<Integer,Date> selectById(String ownerId, String paymentType) throws SQLException {
        List<Payment> payments = paymentDAO.selectById(ownerId, paymentType);
        Map<Integer,Date> map = new HashMap();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for(int i = 0;i<payments.size();i++){
            StringBuffer stringBuffer = new StringBuffer();
            StringBuffer stringBuffer1 = new StringBuffer();
            Payment payment = payments.get(i);
            String[] oldPaymentBalances = payment.getPaymentBalance().split("\\.");
            Integer paymentBalance = Integer.parseInt(oldPaymentBalances[0]);
            String paymentTime1 = payment.getPaymentTime();
            try {
                Date date = sdf.parse(paymentTime1);
                map.put(paymentBalance,date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

//
//            String[] time1 = payment.getPaymentTime().split("\\-");
//
//            System.out.println("------"+ Arrays.toString(time1));
//            for(int j = 0;j<time1.length;j++){
//                stringBuffer.append(j);
//            }
//            String[] time2 = stringBuffer.toString().split("\\:");
//            System.out.println("++++--"+ Arrays.toString(time2));
//            for(int j = 0;j<time2.length;j++){
//                stringBuffer1.append(j);
//            }
//           int paymentTime = Integer.parseInt(stringBuffer1.toString());

        }
        return map;
    }
    @Override
    public boolean deleteById(Integer id) throws SQLException {
        return paymentDAO.deleteById(id);
    }
    @Override
    public boolean removeAll(String[] idsArr) throws SQLException {
        return paymentDAO.deleteAll(idsArr);
    }
    @Override
    public boolean insert(Payment p) throws SQLException {
        return paymentDAO.insert(p);
    }

    @Override
    public Integer count() throws SQLException {
        return paymentDAO.count();
    }

    public Integer countSearch(String search) throws SQLException {
        return paymentDAO.countSearch(search);
    }

    @Override
    public List<Payment> selectAllPagination(Integer offset, Integer limit) throws SQLException {
        return paymentDAO.selectAllPagination(offset,limit);
    }
    @Override
    public List<Payment> selectAllPaginationSearch(Integer offset, Integer limit, String search) throws SQLException {
        return paymentDAO.selectAllPaginationSearch(offset,limit,search);
    }

}